package com.jinyuan.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Environment;

import com.android.threadpool.IActivity;
import com.android.threadpool.ThreadPoolManager;
import com.jinyuan.R;

/**
 * 
 * 
 * @author azuryleaves
 * @version 1.0
 * 
 */
public class MobileApplication extends Application {

	public static String TAG = "Jinyuan";
	public static MobileApplication mobileApplication;
	public static ArrayList<IActivity> allIActivity = new ArrayList<IActivity>();
	public static ArrayList<Activity> allActivity = new ArrayList<Activity>();

	public static ThreadPoolManager poolManager;
	public Receiver rev = new Receiver(); // 监控网络状态（是否在网）
	private Properties http_properties = new Properties();
	public static SharedPreferences preferences;

	@Override
	public void onCreate() {
		super.onCreate();
		mobileApplication = this;
		CrashHandler crashHandler = CrashHandler.getInstance();
//		crashHandler.init(this);
		poolManager = ThreadPoolManager.getInstance(new ClientTask());
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		registerReceiver(rev, filter); // 注册监控网络状态的广播
		try {
			http_properties.load(getResources().openRawResource(R.raw.http));
		} catch (Exception e) {
			e.printStackTrace();
		}
		preferences = getSharedPreferences(TAG, MODE_PRIVATE);
		if (preferences.getBoolean("FirstRun", true)) {
			Iterator<Entry<Object, Object>> iterator = http_properties
					.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<Object, Object> entry = iterator.next();
				String key = (String) entry.getKey();
				String value = (String) entry.getValue();
				preferences.edit().putString(key, value).commit();
			}
			preferences.edit().putBoolean("FirstRun", false).commit();
		}

		File file = new File(Environment.getExternalStorageDirectory()
				.getPath() + File.separator + TAG);
		if (!file.exists()) {
			file.mkdir();
		}
		File logFile = new File(Environment.getExternalStorageDirectory()
				.getPath()
				+ File.separator
				+ TAG
				+ File.separator
				+ CrashHandler.FileName);
		if (!logFile.exists()) {
			logFile.mkdir();
		}
		File updateFile = new File(Environment.getExternalStorageDirectory()
				.getPath() + File.separator + TAG + File.separator + "update");
		if (!updateFile.exists()) {
			updateFile.mkdir();
		}
		File configFile = new File(Environment.getExternalStorageDirectory()
				.getPath() + File.separator + TAG + File.separator + "config");
		if (!configFile.exists()) {
			configFile.mkdir();
		}
	}

	public static IActivity getActivityByName(String name) {
		IActivity ia = null;
		for (IActivity ac : allIActivity) {
			if (name.startsWith(ac.getClass().getName())) {
				ia = ac;
			}
		}
		return ia;
	}

	public static void exitApp(Context context) {
		for (int i = 0; i < allActivity.size(); i++) {
			allActivity.get(i).finish();
		}
		allIActivity.clear();
		allActivity.clear();
		if (poolManager != null) {
			poolManager.destory();
		}
		try {
			MobileApplication.mobileApplication
					.unregisterReceiver(MobileApplication.mobileApplication.rev);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

		String packName = context.getPackageName();
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(ACTIVITY_SERVICE);
		activityMgr.restartPackage(packName);
		android.os.Process.killProcess(android.os.Process.myPid());
	}

}
